package cn.hf.mychat.controller;


import cn.hf.mychat.entity.po.TbSysUser;
import cn.hf.mychat.entity.test.TestEntity;
import cn.hf.mychat.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Date;

@Component
@Slf4j
@RequiredArgsConstructor
public class Listener {


    private final UserMapper userMapper;

    /**
     * spring异步时间监听  @EventListener
     * TODO 1.一直异步刷新监听吗？ 2.主事务回滚  这里监听的推送机制（事务提交后发送/提交前发送？）
     * @Order传入的值越小，执行顺序越高
     * @TransactionalEventListener
     */
    @Async
    @Order(1)
    @EventListener
    public void test(TestEntity entity){
        log.info("进入监听执行==================");
        System.out.println("监听成功=====>"+entity);
        entity.setName("改动");
        entity.setPassword("123455");
        TbSysUser tbSysUser = new TbSysUser();
        tbSysUser.setPhone("11111111");
        tbSysUser.setUserName("hf");
        tbSysUser.setCreateTime(new Date());
        tbSysUser.setUpdateTime(new Date());
        tbSysUser.setPassword("1233");

        int insert = userMapper.insert(tbSysUser);
        System.out.println("insert = " + insert);
    }
    @Async
    @Order(2)
    @EventListener
    public void test02(TestEntity entity){
        log.info("进入监听执行==================");
        System.out.println("监听成功=====>");
        TbSysUser tbSysUser = new TbSysUser();
        tbSysUser.setPhone("11111111");
        tbSysUser.setUserName("hf");
        tbSysUser.setCreateTime(new Date());
        tbSysUser.setUpdateTime(new Date());
        tbSysUser.setPassword("1233");

        int insert = userMapper.insert(tbSysUser);
        System.out.println("insert = " + insert);
    }
}
